package cn.buddha.jec.util;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * @ClassName TongHttpUtils
 * @Author 友野浩二
 * @Date 2020/4/2 23:17
 * @Description Http工具类
 * @Version 1.0
 */
@Slf4j
public class TongHttpUtils {

    /**
     * @return java.lang.String
     * @Author 友野浩二
     * @Description // 获取HTTP请求post方式的body体
     * @Date 2020/4/2 23:23
     * @Param [request]
     */
    public static String getHttpBody(HttpServletRequest request) {
        StringBuilder httpBody = new StringBuilder();

        try (
                ServletInputStream servletInputStream = request.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(servletInputStream, Charset.forName("UTF-8")));
        ) {
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                httpBody.append(line);
            }
        } catch (IOException e) {
            log.info("Get HTTP Body Exception", e);
        }

        return httpBody.toString();
    }

}
